
#ifdef USE_LOADGEN_GUI


#include "fileio.h"
#include "Oxsipgen.h"
#include <QSplashScreen>

FileIO* FileIO::theInstance=0;
FileIO* FileIO::getInstance()
{
    if(0==theInstance)
        theInstance = new FileIO;
    return theInstance;
}


FileIO::FileIO()
{
        //Constructor
    errorStr = "";
}
FileIO::~FileIO()
{
        //Destructor
    delete theInstance;
}

int FileIO::menuOpen()
{
    int retVal = menuClose();
    if(FileIO::OK==retVal){
        QString file = QFileDialog::getOpenFileName(
                0,
        tr("Choose a main scene to open"),
        "",                                    // ok, laat hier dus nooit /home staan, want onder windows crasht em hierop
        tr("Scene Files %1").arg("(*.xml)"));
        currentFile = file;
        retVal=open(currentFile);
    }
    if(FileIO::OK!=retVal)
        errorDialog(retVal);
    return retVal;
}


int FileIO::menuClose()
{
    int isClosed=close();
    if(FileIO::OK==isClosed){
    }
    return isClosed;
}


int FileIO::open(QString filename)
{
        //QString filename = file.section('/',-1);
        //QString path = file.section('/',0,-2);
    qDebug() << "open file " << filename;
    if ("" != filename) emit( loadNewScene(filename) );
    return FileIO::OK;
}

int FileIO::close()
{

    QPixmap pixmap(":/waitforcallsclosing.png");
    QSplashScreen splash(pixmap);
    splash.show();
    Oxsipgen::getInstance()->terminateAllCalls();
    Oxsipgen::getInstance()->waitForThreadsToTerminate();
    return FileIO::OK;
}

void FileIO::errorDialog(int errorNo,QString error)
{
    QString title=tr("I/O error");
    QString text ="text";
    text.setNum(errorNo);
    if(""!=error){
        text.append(": ");
        text.append(error);
    }else{
        text.append(": ");
        switch(errorNo){
            case FileIO::ERROR:
                text.append(tr("General error."));
                break;
            case FileIO::USER_CANCELLED:
                text.append(tr("Cancelled by user."));
                break;
            case FileIO::OPEN_NOT_FOUND:
                text.append(tr("can't find the selected file"));
                break;
            case FileIO::OPEN_CORRUPT_FILE:
                text.append(tr("The selected file is not a valid file"));
                break;
        }
    }
    if(""!=errorStr){
        text.append("\n");
        text.append(errorStr);
        errorStr="";//1st time only
    }
    QMessageBox::critical(0,title,text, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
}


int FileIO::menuHistorySave()
{
    int threadId=-1;
    emit( whatIsTheSelectedThreadId(threadId));
    qDebug() << threadId << endl;
    saveHistoryDialog( threadId );
}
int FileIO::menuHistorySaveAll()
{
    saveHistoryDialog(-1);
}

int FileIO::saveHistoryDialog(int threadId)
{
    QString file = QFileDialog::getSaveFileName(
            0,
    tr("Choose a file to save the history"),
    "",
    tr("Oxsipgen History xml file (*.*)"));
    if(!file.isEmpty()){
        qDebug()<< "file: " << file;
        bool result = Oxsipgen::getInstance()->saveThreadsHistory(file.toStdString(), threadId);
        if (result) {
            QString text;
            if (-1 == threadId) text = tr("The history of all the threads has been saved correctly");
            else text = tr("The history of thread %1 has been saved correctly").arg(threadId+1);
            QMessageBox::information(0,"File saved",text, QMessageBox::Ok, QMessageBox::NoButton, QMessageBox::NoButton);
            return FileIO::OK;
        }
        return FileIO::ERROR;

    }else{
        qDebug()<< "nothing selecteerd > don't save";
        return FileIO::USER_CANCELLED;
    }
}

#endif // USE_LOADGEN_GUI

